package com.example.shop_2212.fgviewmodel

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.example.base.entity.CouponEntityResult
import com.example.base.entity.GetCouponEntityResult
import com.example.base.entity.GoodsEntityResult
import com.example.base.entity.IndexEntityResult
import com.example.base.mvvm.BaseViewModel
import kotlinx.coroutines.launch

class HomeViewModel:BaseViewModel() {
    //返回值--首页数据
    val indexSuccess = MutableLiveData<IndexEntityResult>()
    val indexFail = MutableLiveData<String>()
    fun index(){
        viewModelScope.launch {
            try {
                indexSuccess.value = apiServer.index()
            }catch (e:Exception){
                indexFail.value  = e.message
            }
        }
    }
    //返回值--优惠券
    val couponSuccess = MutableLiveData<CouponEntityResult>()
    val couponFail = MutableLiveData<String>()
    fun coupon(limit:String,page:String,type:String){
        viewModelScope.launch {
            try {
                couponSuccess.value = apiServer.coupon(limit, page, type)
            }catch (e:Exception){
                couponFail.value = e.message
            }
        }
    }
    //返回值--领取优惠券
    val getCouponSuccess = MutableLiveData<GetCouponEntityResult>()
    val getCouponFail = MutableLiveData<String>()
    fun getCoupon(couponId:Int){
        viewModelScope.launch {
            try {
                val map = mutableMapOf<String,Int>()
                map.put("couponId",couponId)
                getCouponSuccess.value = apiServer.getCoupon(mapToBody(map))
            }catch (e:Exception){
                getCouponFail.value = e.message
            }
        }
    }
    //返回值---首页商品
    val indexGoodSuccess = MutableLiveData<GoodsEntityResult>()
    val indexGoodFail = MutableLiveData<String>()
    fun indexGoods(type:Int,limit:Int,page:Int){
        viewModelScope.launch {
            try {
                indexGoodSuccess.value = apiServer.goodsIndex(type, limit, page)
            }catch (e:Exception){
                indexGoodFail.value = e.message
            }
        }
    }
    //返回值---首页商品
    val indexGoodSuccess1 = MutableLiveData<GoodsEntityResult>()
    val indexGoodFail1 = MutableLiveData<String>()
    fun indexGoods1(type:Int,limit:Int,page:Int){
        viewModelScope.launch {
            try {
                indexGoodSuccess1.value = apiServer.goodsIndex(type, limit, page)
            }catch (e:Exception){
                indexGoodFail1.value = e.message
            }
        }
    }
}